package com.hzlj.position.dbsync.common.config;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.exception.PubException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Slf4j
@Data
@Configuration
@ConfigurationProperties("api.position")
public class PositionDbSyncConfig {
    private String url = "http://localhost:10108";
    /**
     * 最大的定位有效期
     * 当腕带的定位时间超过这个时间后，就会提示无效
     */
    private Integer maxMinutes = 10;
    public static Integer staticMaxMinutes = 10;


    @PostConstruct
    private void init() {
        staticMaxMinutes = this.getMaxMinutes();
    }



    /**
     * 是否过期
     */
    public static void checkExpire(String positionTime,Object position) {
        if (ObjectUtil.isEmpty(positionTime)) {
            log.warn("没有定位时间，认为是无效记录:position={}",position);
            throw new PubException("没有定位时间，认为是无效记录").setData(position);
        }
        DateTime locateTime = DateUtil.parse(positionTime);
        DateTime now = DateUtil.date();
        long between = DateUtil.between(locateTime, now, DateUnit.SECOND, true);
        if (between / 60 > staticMaxMinutes) {
            log.warn("定位时间超过最大的有效期，认为是无效记录,now={},positionTime={},position={}", now, locateTime,position);
            throw new PubException("定位时间超过最大的有效期，认为是无效记录").setData(position);
        }
    }


}
